/*
 * Powered By [rapid-framework]
 * Csdn Code: generate simple CRUD code in single table.
 * Since 2015 - 2017
 */

BUI.use('common/page');
BUI.use([ 'common/search', 'bui/overlay', 'bui/grid', 'bui/data', 'bui/form',
    'bui/select','bui/picker' ], function(Search, Overlay, Grid, Data, Form, Select,Picker) {
    // 查询表单
    var form = new Form.Form({
        srcNode : '#searchForm'
    }).render();
    // 定义表头
    columns = [ {
        title : '产品名称',
        dataIndex : 'channelName',
        elCls : 'center',
        width : 80,
        editor : {
            xtype : 'text',
            rules : {
                required : [ false, '名称!' ]
            }
        }
    }, {
        title : '用户注册数',
        dataIndex : 'registerNum',
        elCls : 'center',
        width : 80,
        editor : {
            xtype : 'select',
            rules : {
                required : [ false, '注册数!' ]
            }
        }
    }, {
        title : '下载数',
        dataIndex : 'activationNum',
        elCls : 'center',
        width : 60,
        editor : {
            xtype : 'select',
            rules : {
                required : [ false, '激活数!' ]
            }
        }
    }, {
        title : '申请（资料填写)数',
        dataIndex : 'applyNum',
        elCls : 'center',
        width : 110,
        editor : {
            xtype : 'select',
            rules : {
                required : [ false, '申请数!' ]
            }
        }
    }, {
        title : '提交（审核）数',
        dataIndex : 'submitNum',
        elCls : 'center',
        width : 80,
        editor : {
            xtype : 'text',
            rules : {
                required : [ false, '提交数!' ]
            }
        }
    }, {
        title : '审核通过数',
        dataIndex : 'adoptNum',
        elCls : 'center',
        width : 80,
        editor : {
            xtype : 'text',
            rules : {
                required : [ false, '通过数!' ]
            }
        }
    }, {
        title : '绑卡数',
        dataIndex : 'tiecardNum',
        elCls : 'center',
        width : 80,
        editor : {
            xtype : 'text',
            rules : {
                required : [ false, '绑卡数!' ]
            }
        }
    }, {
        title : '签约(提现申请)数',
        dataIndex : 'signNum',
        elCls : 'center',
        width : 110,
        editor : {
            xtype : 'text',
            rules : {
                required : [ false, '签约数!' ]
            }
        }
    }, {
        title : '放款件数',
        dataIndex : 'loanAmount',
        elCls : 'center',
        width : 80,
        editor : {
            xtype : 'text',
            rules : {
                required : [ false, '放款数!' ]
            }
        }
    }, {
        title : '放款总额',
        dataIndex : 'loanAmountNum',
        elCls : 'center',
        width : 80,
        editor : {
            xtype : 'text',
            rules : {
                required : [ false, '放款金额!' ]
            }
        }
    }, {
        title : '统计开始有效日期 ',
        dataIndex : 'statisticalStartTime',
        elCls : 'center',
        width : 110,
        editor : {
            xtype : 'date',
            datePicker : {
                showTime : true
            },
            rules : {
                required : [ false, '日期不能为空!' ]
            }
        },
        renderer : BUI.Grid.Format.datetimeRenderer
    }, {
        title : '统计结束有效日期 ',
        dataIndex : 'statisticalStartTime',
        elCls : 'center',
        width : 110,
        editor : {
            xtype : 'date',
            datePicker : {
                showTime : true
            },
            rules : {
                required : [ false, '日期不能为空!' ]
            }
        },
        renderer : BUI.Grid.Format.datetimeRenderer
    }, {
        title : '操作',
        elCls : 'center',
        width : 50,
        renderer : function(value, obj) {
            return '<span class="grid-command btn-edit">编辑</span>';
        }
    } ];
    // 渠道选择框
    // var channel="";
    /*	var privileges = sessionStorage.getItem('channelData');
        var privilegeList = privileges.split(',');
        var items = [];
        for (var i = 0; i < privilegeList.length - 1; i++) {
            var privilege = privilegeList[i].split('-');
            items[i] = {
                text : privilege[1],
                value : privilege[0]
            };
        }*/

    $(function() {
        var saveUrl = 'listMarchinProduct';
        jQuery.ajax({
            type : 'POST',
            url : saveUrl,
            dataType : 'json',
            contentType : 'application/json;charset=UTF-8',
            complete : function(data) {
                var itemss = [];
                var partList = jQuery.parseJSON(data.responseText);
                for (var i = 0; i < partList.length; i++) {
                    var privileges = partList[i];
                    var productName=privileges.productName;
                    var productId = privileges.productId;
                    // var oobbj = [];
                    // oobbj[0] = productName;
                    // oobbj[1] = productId;
                    itemss[i] = {};
                    itemss[i]['text'] = productName;
                    itemss[i]['value'] = productId;
                }
                var suggest = new Select.Suggest({
                    render:'#channelDatas',
                    value: '#channelName',
                    data:itemss
                });
                suggest.render();
                suggest.on('change', function(ev) {
                    $(".channelNames").val(ev.text);
                    $("input[name='productId']").val(ev.value);
                });
                var suggests = new Select.Suggest({
                    render:'#channelSelect',
                    value: '#channelName',
                    data:itemss
                });
                suggests.render();
                suggests.on('change', function(ev) {
                    $(".chanleSele").val(ev.text);
                });
            }
        });
    });


    var title = '新增或修改';
    editing = new Grid.Plugins.DialogEditing({
        contentId : 'content', // 设置隐藏的Dialog内容
        triggerCls : 'btn-edit', // 触发显示Dialog的样式
        editor : {
            title : title,
            width : '900',

        }
    }),
        // 获取数据
        store = new BUI.Data.Store({
            url : 'listchannelPages?direction=desc',
            pageSize : 20,
            autoLoad : true,
            remoteSort : true
        });

    // 获取表格对象
    gridCfg = Search.createGridCfg(columns, {
        plugins : [editing,Grid.Plugins.CheckSelection,Grid.Plugins.ColumnResize] , // 
        //plugins : [ editing, Grid.Plugins.CheckSelection ],
        emptyDataTpl : '<div class="centered"><img alt="Crying" src="http://img03.taobaocdn.com/tps/i3/T1amCdXhXqXXXXXXXX-60-67.png"><h2>查询的数据不存在</h2></div>',
        tbar : { // 添加、删除
            items : [ {
                btnCls : 'button button-small',
                text : '<i class="icon-plus"></i>添加',
                listeners : {
                    'click' : addFunction
                }
            }, {
                btnCls : 'button button-small',
                text : '<i class="icon-refresh"></i>刷新',
                listeners : {
                    'click' : refreshFunction
                }
            }, {
                btnCls : 'button button-small',
                text : '<i class="icon-remove"></i>删除',
                listeners : {
                    'click' : delFunction
                }
            } ]
        },
    });
    // 添加记录
    function addFunction() {
        var newData = {
            b : 0
        };
        editing.edit(newData, 'channelName'); // 添加记录后，直接编辑
    }
    // 刷新
    function refreshFunction() {
        search.load();
    }

    editing.on('accept', function(ev) {
        var record = editing.get('record');
        var none = record["channelId"] === "";
        var saveUrl = none ? 'saveChannelStatisticsExternal'
            : 'updateChannelStatisticsExternal';
        var requestType = none ? 'POST' : 'PUT';
        jQuery.ajax({
            type : requestType,
            url : saveUrl,
            dataType : 'json',
            contentType : 'application/json;charset=UTF-8',
            data : JSON.stringify(record),
            complete : function(data) {
                if (data.responseText == "success") { // 删除成功
                    search.load();
                } else { // 删除失败
                    BUI.Message.Alert('保存失败！');
                }
            }
        });
    });
    // 删除选中的记录
    function delFunction() {
        var selections = grid.getSelection();
        delItems(selections);
        store.remove(selections);
    }
    $('#btnclick').click(function(){
        $(".chanleSele").val('');
    });
    // 删除
    function delItems(items) {
        var ids = "";
        BUI.each(items, function(item) {
            ids += item.channelId + ",";
        });
        if (ids.length) {
            BUI.Message.Confirm('确认要删除选中的记录么？', function() {
                jQuery.ajax({
                    type : 'DELETE',
                    url : 'removeChannelStatisticsExternal?ids=' + ids,
                    dataType : 'json',
                    data : {},
                    complete : function(data) {
                        if (data.responseText == "success") { // 删除成功
                            search.load();
                        } else { // 删除失败
                            BUI.Message.Alert('删除失败！');
                        }
                    }
                });
            }, 'question');
        }
    }
    search = new Search({
        width : '100%',// 这个属性一定要设置
        store : store,
        autoSearch : true,
        gridCfg : gridCfg
    });
    grid = search.get('grid');
});
